package com.hengtiansoft.lfy.chart.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hengtiansoft.lfy.R;
import com.hengtiansoft.lfy.chart.utils.UserUtils;

import java.util.ArrayList;
import java.util.HashMap;

public class SearchFriendsAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<HashMap<String, Object>> mList;
    SearchFriendsAdapterListener mListener;

    public void setAdapterListener(SearchFriendsAdapterListener listener) {
        this.mListener = listener;
    }

    public SearchFriendsAdapter(Context context,
                                ArrayList<HashMap<String, Object>> list) {
        mContext = context;
        this.mList = list;
        mInflater = LayoutInflater.from(mContext);
    }

    public final class ViewHolder {
        TextView mTvName;
        ImageView mIvAvatar;
        RelativeLayout mRlAddFriends;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public HashMap<String, Object> getItem(int position) {
        return mList.get(position);
    }

    @Override
    public View getView(final int position, View convertView,
                        ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.activity_add_contact_search_listview_item, null);
            holder.mTvName = (TextView) convertView.findViewById(R.id.name);
            holder.mIvAvatar = (ImageView) convertView
                    .findViewById(R.id.avatar);
            holder.mRlAddFriends = (RelativeLayout) convertView
                    .findViewById(R.id.rl_add_friends);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        HashMap<String, Object> item = getItem(position);
        holder.mTvName.setText(item.get("Username").toString());
        if (item.get("Imagepath") != null
                && !item.get("Imagepath").toString().equals("")) {
            UserUtils.setCurrentImagePath(mContext, holder.mIvAvatar,
                    item.get("Imagepath").toString());
        }
        holder.mRlAddFriends.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.addClick(position);
            }
        });
        convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (mListener != null)
                    mListener.onItemClick(position);
            }
        });
        return convertView;
    }

    public interface SearchFriendsAdapterListener {
        public void addClick(int position);

        public void onItemClick(int position);

    }
}
